<template>
  <a-modal
    title="考勤规则设置"
    :width="500"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="入园时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-time-picker class="fullwidth" :default-value="moment('8:30', 'HH:mm')" format="HH:mm" />
        </a-form-item>
        <a-form-item
          label="离园时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-time-picker class="fullwidth" :default-value="moment('16:30', 'HH:mm')" format="HH:mm" />
        </a-form-item>

      </a-form>

    </a-spin>
  </a-modal>
</template>

<script>
  import { courseTypeEdit } from '@/api/modular/garden/courseManage'
import moment from 'moment';

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true;
        this.$nextTick(() => {
          
          // this.form.setFieldsValue(
          //   {
          //     id: record.id,
          //     name: record.name,
          //     isPublic: record.isPublic,
          //     sort: record.sort,
          //     description: record.description,
          //   }
          // )
        })
      },

      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true;
        validateFields((errors, values) => {
          if (!errors) {
            if(values.sort < 0){this.$message.error('排序请填写大于0的整数'); return;}
            values.sort = parseInt(values.sort) 
            courseTypeEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.visible = false
                this.confirmLoading = false
                this.$emit('ok', values)
                this.form.resetFields()
              } else {
                this.$message.error('编辑失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
